
<template>

	<a-modal
		v-model:open="visible"
		title="选择单据"
		:width="1200"
		:mask-closable="false"
		:destroy-on-close="true"
		@ok="handleOk"
		@close="handleClose"
		@cancel="handleClose"
	>


		<a-card :bordered="false">

			<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
				<a-row :gutter="24">
					<a-col :span="6">
						<a-form-item label="单据编号" name="productTypeName">
							<a-input v-model:value="searchFormState.billOrderNo" placeholder="请输入单据编号" />
						</a-form-item>
					</a-col>
					<a-col :span="6">

						<a-form-item label="单据类型" name="billType">
							<a-select  v-model:value="searchFormState.billType" placeholder="请选择单据类型" allow-clear :options="typeOptions" />
						</a-form-item>
					</a-col>
					<a-col :span="8">

						<a-form-item label="单据日期" name="billDate">
							<a-range-picker v-model:value="searchFormState.billDate" />
						</a-form-item>
					</a-col>
					<a-col :span="4">
						<a-button type="primary" @click="loadData">查询</a-button>
						<a-button style="margin: 0 8px" @click="reset">重置</a-button>
					</a-col>
				</a-row>
			</a-form>

			<a-table
				ref="tableRef"
				:columns="columns"
				:alert="options.alert.show"
				:data-source="tableData"
				:pagination="false"

				bordered
				:row-key="(record) => record.billId"
				:row-selection="options.rowSelection"
				:rowSelection="{type: 'radio',onChange: rowSelectedChange,selectedRowKeys: selectedInputItemValue,}"
			>
			</a-table>

		</a-card>

	</a-modal>

</template>

<script setup name="billheader">
import { cloneDeep } from 'lodash-es'
import imsBillApi from '@/api/ims/imsBillApi'
import imsOrderApi from "@/api/ims/imsOrderApi";
import tool from "@/utils/tool";


const visible = ref(false)
const tableData = ref([])
const props = defineProps({
	type: {
		type: String,
		default: () => 'bill'
	}
})

const typeOptions = ref([])
const searchFormState = ref({})
const tableRef = ref()
const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
const columns = [
	{
		title: '单据编号',
		dataIndex: 'billOrderNo'
	},
	{
		title: '原始单号',
		dataIndex: 'oldOrderNo'
	},
	{
		title: '类型',
		dataIndex: 'billType'
	},
	{
		title: '单据日期',
		dataIndex: 'billDate'
	},
	{
		title: '开单日期',
		dataIndex: 'createTime'
	},
	{
		title: '发票类型',
		dataIndex: 'invoiceType'
	},
	{
		title: '业务员',
		dataIndex: 'user.name',
		customRender: ({ record }) => record.user?.name
	},
	{
		title: '客户',
		dataIndex: 'customer.customerName',
		customRender: ({ record }) => record.customer?.customerName
	},
	{
		title: '备注',
		dataIndex: 'orderRemark'
	},

]

// 默认选中
const selectedRowKeys = ref([])
// 已选中
const selectedBrent = ref(null)
const selectedInputItemValue = ref([]);
const rowSelectedChange = (selectedRowKeys, selectRows) => {
	selectedBrent.value = selectedRowKeys[0];
	selectedInputItemValue.value = selectedRowKeys;

}


const emit = defineEmits(['successful'])



const handleOk = () => {
	const selectedItem = tableData.value.find(item => item.billId === selectedBrent.value)
	emit('successful', selectedItem)
	visible.value = false
}
const handleClose = () => {

}
const onOpen = (data) => {
	visible.value = true

	typeOptions.value = tool.dictList('bill_type')
	typeOptions.value.push(...tool.dictList('finance_type'))
}




// 列表选择配置
const options = {
	// columns数字类型字段加入 needTotal: true 可以勾选自动算账
	alert: {
		show: true,
		clear: () => {
			selectedRowKeys.value = ref([])
		}
	},
	rowSelection: {
		onChange: (selectedRowKey, selectedRows) => {
			selectedRowKeys.value = selectedRowKey
		}
	}
}
const loadData = (parameter) => {
	console.log('查询类型：', props.type)
	const searchFormParam = cloneDeep(searchFormState.value)
	imsBillApi.getAllBill(searchFormParam).then((data) => {
		tableData.value = data
		console.log('获取查询全部单据的数据：',data)
	})
}
// 重置
const reset = () => {
	searchFormState.value = {}
	loadData()
}
loadData()

defineExpose({
	onOpen
})
</script>
<style>
.detail-header-row {
	margin-top: 40px;
}

.bill-detail-title {
	margin-top: 40px;
	color: rgba(0, 0, 0, 0.88);
	font-weight: 600;
	font-size: 16px;
	line-height: 1.5;
	word-wrap: break-word;
}
</style>
